﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PFMConfigurations;

namespace PFMMain
{
    public partial class PFMMainForm : Form
    {
        PFMConfigurations.PFMConfigurationManager cm = null;        
        public PFMMainForm()
        {
            InitializeComponent();
            this.Text = "PFM 启动于" + DateTime.Now.ToLongDateString();           
            LoadData();
        }
        private void LoadData()
        {
           
            if(cm==null)
                cm = new PFMConfigurations.PFMConfigurationManager();
            //cm.LoadTargets();           
            this.targetBindingSource.DataSource = cm.Targets;
            //InitializeDataBinding();
        }

        private void 保存SToolStripButton_Click(object sender, EventArgs e)
        {
            if (cm != null)
                cm.SaveTarget();         
        }

        private void RuntoolStripButton_Click(object sender, EventArgs e)
        {
            Target t = cm.Targets[this.targetBindingSource.Position];
            PFMKernels.PFMCoordinator coor = new PFMKernels.PFMCoordinator();
            try
            {
                coor.DoSpecifiedTask(t);
            }
            catch
            {
            }

        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                StringBuilder result = new StringBuilder();
                if (e.Value is List<String>)
                {
                    List<String> s = (List<String>)e.Value;
                    foreach (string item in s)
                    {
                        result.Append(item);
                        result.Append(";");
                    }
                    if(result.Length>=1)
                      result.Remove(result.Length - 1, 1);
                }
                e.Value = result.ToString();
            }
        }

        private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            
            if (e.ColumnIndex == 1)
            {
                //MessageBox.Show(e.DesiredType.ToString());
                e.Value = PFMGlobals.PFMDefinitions.ToList<String>((String)e.Value, ";");
                e.ParsingApplied = true;
            }
           
        }
       
    }
}
